#include <bits/stdc++.h>

using namespace std;
const int N = 1010;
const int M = 110;
int f[N][M][2];
class Solution
{
public:
    int maxProfit(int k, vector<int> &prices)
    {
        memset(f, -0x3f, sizeof(f));
        for (int i = 0; i <= prices.size(); ++i)
        {
            f[i][0][0] = 0;
        }
        for (int i = 1; i <= prices.size(); ++i)
        {
            for (int j = 1; j <= k; ++j)
            {
                f[i][j][0] = max(f[i - 1][j][0], f[i - 1][j][1] + prices[i - 1]);
                f[i][j][1] = max(f[i - 1][j][1], f[i - 1][j - 1][0] - prices[i - 1]);
            }
        }
        int ret = 0;
        for (int j = 0; j <= k; ++j)
        {
            ret = max(ret, f[prices.size()][j][0]);
        }
        return ret;
    }
};